package pareto;

import java.util.ArrayList;
import java.util.List;

public class ParetoSort {
	public static List sortPopulation(List<List> valList){
		List indexList = new ArrayList();
		
		indexList.add(0);
		
		for(int i=1;i<valList.size();i++){
			List currentSolution = valList.get(i);
			
			boolean isInserted = false;
			
			for(int j=0;j<indexList.size();j++){
				List thisSolution = valList.get((Integer)indexList.get(j));
				if(compare(thisSolution,currentSolution)>0){
					indexList.add(j, i);
					isInserted = true;
					break;
				}
			}
			
			if(!isInserted){
				indexList.add(indexList.size(), i);
			}
		}
		
		return indexList;
	}
	
	public static int compare(List s1, List s2){
		int larger = 0;
		int smaller = 0;
		int equal = 0;
		
		for(int i=0;i<s1.size();i++){
			if((Double)s1.get(i)<(Double)s2.get(i)){
				smaller++;
			}else if((Double)s1.get(i)>(Double)s2.get(i)){
				larger++;
			}else{
				equal++;
			}
		}
		
		if(larger > 0 && smaller ==0){
			return 1;
		}
		
		if(larger == 0 && smaller >0){
			return -1;
		}
		
		return 0;
	}
	
	public static double getDistance(List val1 , List val2){
		double distance = 0.0;
		for(int i=0;i<val1.size();i++){
			distance += Math.pow(((Double)val1.get(i)-(Double)val2.get(i)), 2);
		}
		return Math.sqrt(distance);
	}
	
	public static boolean checkIsContained(List<List> population, List solution){
		boolean result = false;
		for(int i=0;i<population.size();i++){
			List currentSolution = population.get(i);
			boolean isContained = true;
			for(int j=0;j<solution.size();j++){
				double d1 = (Double)solution.get(j);
				double d2 = (Double)currentSolution.get(j);
				
				if(d1!=d2){
					isContained = false;
					break;
				}			
			}
			if(isContained){
				result  = true;
				return result;
			}
		}
		return result;
	}
	
	
	public static void main(String[] args){
		List<List> population = new ArrayList();
		List s1 = new ArrayList();
		s1.add(1.2);
		s1.add(2.4);
		
		List s2 = new ArrayList();
		s2.add(3.2);
		s2.add(4.1);
		
		population.add(s1);
		population.add(s2);
		
		List s3 = new ArrayList();
		s3.add(1.2);
		s3.add(2.4);
		
		System.out.println(ParetoSort.checkIsContained(population, s3));
	}
}
